package main

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
)

func main() {
	//"用户名:密码@[连接方式](主机名:端口号)/数据库名"
	db, _ := sql.Open("mysql", "root:root@(172.16.40.233)/world") // 设置连接数据库的参数
	defer db.Close()                                              //关闭数据库
	err := db.Ping()                                              //连接数据库
	if err != nil {
		fmt.Println("数据库连接失败") //连接失败
		return
	} else {
		fmt.Println("数据库连接成功") //连接成功
	}

	rows, _ := db.Query("select * from city") //获取city表所有数据
	var ID, Population int
	var Name, CountryCode, District string
	for rows.Next() { //循环显示所有的数据
		rows.Scan(&ID, &Name, &CountryCode, &District, &Population)
		fmt.Println(ID, "--", Name, "--", CountryCode, "--", District, "--", Population)
	}
}
